package com.jp.knowledge.my.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.MotionEvent;
import android.view.TouchDelegate;
import android.view.View;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.my.model.OrganizaModel;

import java.util.List;

public class OrganizationAdapter extends RecycleVAdapter<OrganizaModel> implements View.OnClickListener, View.OnTouchListener {

    private static final int ORGANIZE_EXIT = 0x01;              // 退出
    private static final int ORGANIZE_CLAIM = 0x02;             // 认领
    private static final int ORGANIZE_BAY_VIP = 0x03;           // 购买会员
    private static final int ORGANIZE_REVIEW_DETAIL = 0x04;     // 审核详情

    private boolean editOrganize;
    private ItemTouchHelper itemTouchHelper;

    public OrganizationAdapter(Context context, @NonNull List<OrganizaModel> list) {
        super(context, list);
        this.editOrganize = false;
    }

    public void setEditOrganization(boolean editOrganize){
        this.editOrganize = editOrganize;
    };
    public boolean getEditOrganize(){
        return this.editOrganize;
    }

    public void setItemTouchHelper(ItemTouchHelper itemTouchHelper){
        this.itemTouchHelper = itemTouchHelper;
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_organize;
    }

    @Override
    public void onBind(RecycleVHolder holder, int position) {
        OrganizaModel model = getData(position);
        String companyName = model.getCompanyFullName();
        if(companyName == null || companyName.trim().length() == 0)
            companyName = "未知";
        holder.setTextView(R.id.item_organize_name, companyName);

        int companyType = model.getCompanyType();           // 组织类型
        int auditStete = model.getAuditStete();             // 审核状态
        int companyManage = model.getCompanyManage();       // 是否是组织管理者
        int companyState = model.getCompanyState();         // 组织状态

        String organizeStateStr = "";
        int color = mContext.getResources().getColor(R.color.google_red);
        TextView organizeState = holder.getTextView(R.id.item_organize_state);
        if(companyState == 0){              // 未认领，先认领
            organizeStateStr = "未认领" + getOrganizeType(companyType);
            color = mContext.getResources().getColor(R.color.google_red);
        } else if (companyState == 1){      // 已经认领
            if(auditStete == 0){            // 待审核
                organizeStateStr = "已认领" + getOrganizeType(companyType);
                color = mContext.getResources().getColor(R.color.font_gray_light);
            } else if(auditStete == 1){     // 审核通过，等级为真
                organizeStateStr = "已认领" + getOrganizeType(companyType);
                color = mContext.getResources().getColor(R.color.font_gray_light);
            } else if(auditStete == 2){     // 审核不通过
                organizeStateStr = "加入审核未通过，请查看原因";
                color = mContext.getResources().getColor(R.color.google_red);
            }
        } else if(companyState == 2){       // 认领中
            organizeStateStr =  getOrganizeType(companyType) + "认领审核中";
            color = mContext.getResources().getColor(R.color.google_red);
        } else if(companyState == 3){       // 认领失败
            organizeStateStr =  getOrganizeType(companyType) + "认领失败，请重新认领";
            color = mContext.getResources().getColor(R.color.google_red);
        }
        organizeState.setText(organizeStateStr);
        organizeState.setTextColor(color);

        if(editOrganize) {
            View sortView = holder.get(R.id.sort);
            sortView.setVisibility(View.VISIBLE);
            sortView.setTag(holder);
            sortView.setOnTouchListener(this);
        } else {
            holder.get(R.id.sort).setVisibility(View.GONE);
        }

        setBnState(holder.get(R.id.item_organize_operating_btn), position);
    }

    private String getOrganizeType(int companyType){
        if(companyType == 1) {
            return "企业";
        } else if(companyType == 2){
            return "学校";
        } else if(companyType == 3){
            return "众创空间";
        } else if(companyType == 4){
            return "组织";
        } else{
            return "";
        }
    }

    private void setBnState(View btn, int position){
        TextView buttom = (TextView) btn;
        OrganizaModel data = getData(position);
        btn.setTag(-1);
        if(this.editOrganize){
            buttom.setText("退出组织");
            buttom.setTextColor(mContext.getResources().getColor(R.color.theme_color));
            buttom.setBackground(mContext.getResources().getDrawable(R.drawable.organiza_blue_press));
            btn.setTag(position*10 + ORGANIZE_EXIT);
        } else if(data.getCompanyState() == 0){
            buttom.setText("认领组织");
            buttom.setTextColor(mContext.getResources().getColor(R.color.google_red));
            buttom.setBackground(mContext.getResources().getDrawable(R.drawable.organiza_red));
            btn.setTag(position*10 + ORGANIZE_CLAIM);
        } else if(data.getCompanyState() == 1){         // 已经认领
            if(data.getAuditStete() == 0){              // 是企业会员
                buttom.setText("加入待审核中");
                buttom.setTextColor(mContext.getResources().getColor(R.color.gray));
                buttom.setBackground(mContext.getResources().getDrawable(R.drawable.organiza_gray));
            } else if(data.getAuditStete() == 1){       // 不是企业会员
                if(data.getCompanyLevel() != null && data.getCompanyLevel().trim().length() > 0){
                    buttom.setText("已加入");
                    buttom.setTextColor(mContext.getResources().getColor(R.color.gray));
                    buttom.setBackground(mContext.getResources().getDrawable(R.drawable.organiza_gray));
                } else {
                    buttom.setText("购买企业会员");
                    buttom.setTextColor(mContext.getResources().getColor(R.color.theme_color));
                    buttom.setBackground(mContext.getResources().getDrawable(R.drawable.organiza_blue_press));
                    btn.setTag(position*10 + ORGANIZE_BAY_VIP);
                }
            } else if(data.getAuditStete() == 2){       // 审核不通过
                buttom.setText("审核详情");
                buttom.setTextColor(mContext.getResources().getColor(R.color.google_red));
                buttom.setBackground(mContext.getResources().getDrawable(R.drawable.organiza_red));
                btn.setTag(position*10 + ORGANIZE_REVIEW_DETAIL);
            }
        } else if(data.getCompanyState() == 2){         // 认领中
            buttom.setText("认领审核中");
            buttom.setTextColor(mContext.getResources().getColor(R.color.gray));
            buttom.setBackground(mContext.getResources().getDrawable(R.drawable.organiza_gray));
        } else if(data.getCompanyState() == 3){         // 认领失败
            buttom.setText("认领失败");
            buttom.setTextColor(mContext.getResources().getColor(R.color.google_red));
            buttom.setBackground(mContext.getResources().getDrawable(R.drawable.organiza_red));
            btn.setTag(position*10 + ORGANIZE_CLAIM);
        }
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(this.organizationButtonClickListener == null || (int)v.getTag() == -1)
            return;
        int position = (int) v.getTag() / 10;
        switch ((int) v.getTag() % 10) {
            case ORGANIZE_EXIT:             // 退出组织
                organizationButtonClickListener.organizeExit(position);
                break;
            case ORGANIZE_CLAIM:            // 认领组织
                organizationButtonClickListener.organizeClaim(position);
                break;
            case ORGANIZE_BAY_VIP:          // 购买会员
                organizationButtonClickListener.organizeByVip(position);
                break;
            case ORGANIZE_REVIEW_DETAIL:    // 审核详情
                organizationButtonClickListener.organizeReviewDetail(position);
                break;
        }
    }

    private OrganizationButtonClickListener organizationButtonClickListener;
    public void setOnSwitchToDefaultListener(OrganizationButtonClickListener organizationButtonClickListener){
        this.organizationButtonClickListener = organizationButtonClickListener;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(itemTouchHelper != null) {
            RecyclerView.ViewHolder viewHolder = (RecyclerView.ViewHolder) v.getTag();
            itemTouchHelper.startDrag(viewHolder);
        }
        return false;
    }

    public interface OrganizationButtonClickListener {
        void organizeExit(int position);
        void organizeClaim(int position);
        void organizeByVip(int position);
        void organizeReviewDetail(int position);
    }
}
